package com.xuecheng.content.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.model.dto.SaveTeachPlanDto;
import com.xuecheng.content.model.dto.TeachPlanDto;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.content.service.TeachPlanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeachPlanServiceImpl implements TeachPlanService {

    @Autowired
    private TeachplanMapper teachplanMapper;

    @Autowired
    private TeachplanMediaMapper teachplanMediaMapper;
    @Override
    public List<TeachPlanDto> selectTreeNodes(Long courseId) {
        return teachplanMapper.selectTreeNodes(courseId);
    }

    @Override
    public void saveTeachPlan(SaveTeachPlanDto saveTeachPlanDto) {
    //通过课程计划id判断是增加还是修改
        Long id = saveTeachPlanDto.getId();
        if(id == null){
            //课程计划id为空，说明是新增，就把传进来的数据赋值给TeachPlan,然后添加
            Teachplan teachplan = new Teachplan();
            BeanUtils.copyProperties(saveTeachPlanDto,teachplan);
            //确定排序字段orderby ,找到当前节点的同一层级的节点的个数，排序字段就是个数加一
            //select count(1) from teachplan where id = parent.id,
            teachplan.setOrderby(getTeachPlanCount(saveTeachPlanDto));
            teachplan.setStatus(0);
           teachplanMapper.insertTeachPlan(teachplan);
            System.out.println(teachplan);
            id = teachplan.getId();
            Teachplan newTeachPlan = teachplanMapper.queryTeachePlan(id);
            System.out.println(newTeachPlan);
            createNewTeachPlan(newTeachPlan);
        }else{
            //saveTeachplan != null,说明就是修改
            Teachplan teachplan = teachplanMapper.selectById(id);
            BeanUtils.copyProperties(saveTeachPlanDto,teachplan);
            teachplanMapper.updateById(teachplan);
        }
    }

    //新增一个小结
    public void createNewTeachPlan(Teachplan teachplan){
        Teachplan newTeachplan = new TeachPlanDto();
        BeanUtils.copyProperties(teachplan,newTeachplan);
        newTeachplan.setPname("新增小结名称[点击修改]");
        newTeachplan.setParentid(teachplan.getId());
        newTeachplan.setGrade(2);
        //确定排序字段orderby ,找到当前节点的同一层级的节点的个数，排序字段就是个数加一
        //select count(1) from teachplan where id = parent.id,
        Long parentId = newTeachplan.getParentid();
        Long courseId = newTeachplan.getCourseId();
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getCourseId, courseId).eq(Teachplan::getParentid, parentId);
        Integer count = teachplanMapper.selectCount(queryWrapper);
        newTeachplan.setOrderby(count + 1);
        newTeachplan.setStatus(0);
        teachplanMapper.insert(newTeachplan);
    }
    /**
     * 删除课程计划
     * @param id
     * @return
     */
    @Override
    public boolean deleteTeachPlan(Long id) {

        Teachplan teachplan = teachplanMapper.selectById(id);
        //如果是章节内容需要判断章节下面是否还有小章节，如果有，删除不成功，如果没有才可以删除
        //1.判断是否为大章节
        if(teachplan.getParentid() == 0){
            //如果是大章节，就查找大章节下面是否有小章节，
            List<Teachplan> teachplanList = teachplanMapper.selectParent(id);
            if(teachplan != null){
                XueChengPlusException.cast("课程计划信息还有子级信息，无法操作");
            }else {
                //如果没有小的章节就进行删除
                teachplanMapper.deleteById(id);
                teachplanMediaMapper.deleteByTeachPlanId(id);
                return true;
            }
        }
        //如果是小章节，就需要把小章节自己和另外一张表的媒资相关的内容删除
        teachplanMapper.deleteById(id);
        teachplanMediaMapper.deleteByTeachPlanId(id);
        return true;
    }

    @Override
    public void moveTeachPlan(String move, Long id) {
        //如果是第一个章节，不能向上移动，如果是最后一个章节，不能向下移动
        Teachplan teachplan = teachplanMapper.selectById(id);
        Integer orderby = teachplan.getOrderby();
        if(move.equals("moveup")){
            Integer moveupId = orderby - 1;
            if(orderby == 1){
                return;
            }else{
                teachplan.setOrderby(orderby - 1);
            }
        }
    }


    private int getTeachPlanCount(SaveTeachPlanDto saveTeachPlanDto){
        //确定排序字段orderby ,找到当前节点的同一层级的节点的个数，排序字段就是个数加一
        //select count(1) from teachplan where id = parent.id,

        Long parentId = saveTeachPlanDto.getParentid();
        Long courseId = saveTeachPlanDto.getCourseId();
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getCourseId, courseId).eq(Teachplan::getParentid, parentId);
        Integer count = teachplanMapper.selectCount(queryWrapper);
        return count + 1;
    }
}
